<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home/welcome' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20" class="search">
        <el-col :span="8">
          <!-- 搜索区域 -->
          <el-input
            placeholder="请输入内容"
            v-model="queryGoods.query"
            clearable
            @keyup.enter.native="query"
            @clear="empty"
          >
            <!-- 搜索按钮 -->
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchBtn"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 添加商品区域 -->
          <el-button type="primary" @click="$router.push('addgoods')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" stripe border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="95px">
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="95px"
        ></el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="170px"
        ></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="editBtn(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteBtn(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="goodsListChange"
        @current-change="goodsListCurrentChange"
        :current-page="queryGoods.pagenum"
        :page-sizes="[1, 5, 10, 15]"
        :page-size="queryGoods.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      title="编辑商品"
      :visible.sync="editVisible"
      width="50%"
      @close="resetEditForm"
    >
      <!-- 编辑表单 -->
      <el-form :model="editForm" :rules="editRules" ref="editFormRef">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格(元)" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSet">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getGoodsListApi, getEditGoodsApi, getDeleteApi, getIdQueryGoodsApi } from '@/api/goods.js'

import moment from 'moment'

export default {
  name: 'GoodsList',
  data() {
    return {
      // 查询参数对象
      queryGoods: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      // 商品列表数组
      goodsList: [],
      // 总共商品条数
      total: 0,
      // 控制编辑对话框显示隐藏
      editVisible: false,
      // 当前编辑的商品数据对象
      editForm: {},
      // 编辑商品的表单效验
      editRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格(元)', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getGoodsListApi()
  },
  methods: {
    // 商品列表数据请求
    getGoodsListApi() {
      getGoodsListApi(this.queryGoods).then((res) => {
        if (!res.data.meta.status === 200) {
          return this.$message.error('获取商品列表失败')
        }
        this.$message.success('获取商品列表成功')
        // 循环商品列表
        res.data.data.goods.forEach((item) => {
          // 转换添加时间的格式
          item.add_time = moment(item.add_time).format('YYYY-MM-DD HH:mm:ss')
        })
        // 赋值
        this.goodsList = res.data.data.goods
        this.total = res.data.data.total
      })
    },
    // 编辑商品请求
    getEditGoodsApi(id, data) {
      getEditGoodsApi(id, data).then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error('编辑商品失败')
        }
        this.$message.success('编辑商品成功')
        // 刷新页面
        this.getGoodsListApi()
      })
    },
    // 根据id查询商品请求
    getIdQueryGoodsApi(id) {
      getIdQueryGoodsApi(id).then(res => {
        // 赋值
        this.editForm = res.data.data
      })
    },
    // 商品删除请求
    getDeleteApi(id) {
      getDeleteApi(id).then((res) => {
        if (!res.data.meta.status === 200) {
          this.$message.error('删除失败')
          return
        }
        this.$message.success('删除成功')
        // 刷新商品列表
        this.getGoodsListApi()
      })
    },
    // 页数改变触发
    goodsListChange(pagesize) {
      this.queryGoods.pagesize = pagesize
      this.getGoodsListApi()
    },
    // 页码改变触发
    goodsListCurrentChange(pagenum) {
      this.queryGoods.pagenum = pagenum
      this.getGoodsListApi()
    },
    // 查询按下enter触发
    query() {
      this.getGoodsListApi()
    },
    // 清空触发
    empty() {
      this.getGoodsListApi()
    },
    // 搜索按钮触发
    searchBtn() {
      this.getGoodsListApi()
    },
    // 删除按钮触发
    deleteBtn(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.getDeleteApi(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击编辑按钮触发
    editBtn(row) {
      // 调用根据id查询商品
      this.getIdQueryGoodsApi(row.goods_id)
      this.editVisible = true
    },
    // 编辑对话框关闭触发
    resetEditForm() {
      // 重置整个编辑表单
      this.$refs.editFormRef.resetFields()
    },
    // 点击编辑对话框的确定按钮触发
    editSet() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) {
          return this.$message.error('请输入表单内容')
        }
        // 调用编辑商品请求
        this.getEditGoodsApi(this.editForm.goods_id, this.editForm)
        // 关闭编辑对话框
        this.editVisible = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
}
.search {
  margin-bottom: 15px;
}
</style>
